<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2018/12/26
 * Time: 13:37
 */

namespace app\sports_admin\controller;
use app\sports_admin\model\Train as TrainModel;
use think\Request;
use app\sports_admin\model\TrainLesson;
use app\sports_admin\model\TrainComment;
use app\exception\TrainException;
use app\sports_admin\model\Customer as CustomerModel;
use app\sports_admin\vaildate\Train as TrainVaildate;
use app\sports_admin\service\Train as TrainService;
class Train extends Base
{
    //培训机构列表页
    public function index()
    {
        return view('index');
    }
    //培训机构列表
    public function trainList()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder','searchName']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $searchName = trim($params['searchName']);
        $result = TrainModel::trainList($page,$rows,$sort,$sortOrder,$searchName);
        return json($result);
    }
    //培训机构课程列表页
    public function trainLessonPage()
    {
        $data = Request::instance()->only('id');
        return view("lesson",compact("data"));
    }
    //培训机构删除课程
    public function trainLessonDel(Request $request)
    {
        if($request->isAjax()){
            $data = TrainLesson::trainLessonDel();
            if(!$data){
                throw new TrainException([
                    'msg'=>'内容信息删除失败',
                    'errorCode'=>400]);
            }
            return json(['msg'=>'删除成功','code'=>200]);
        }
    }
    //培训机构课程列表
    public function trainLesson()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder','searchName','trainId']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $searchName = trim($params['searchName']);
        $trainId    = trim($params['trainId']);
        $result = TrainLesson::trainLesson($page,$rows,$sort,$sortOrder,$searchName,$trainId);
        return json($result);
    }
    //培训机构评论列表页
    public function trainCommentPage()
    {
        $data = Request::instance()->only('id');
        return view("comment",compact("data"));
    }
    //培训机构评论列表
    public function trainComment()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder','searchName','trainId']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $searchName = trim($params['searchName']);
        $trainId    = trim($params['trainId']);
        $result = TrainComment::trainList($page,$rows,$sort,$sortOrder,$searchName,$trainId);
        return json($result);
    }
    //培训机构添加评论页面
    public function trainCommentAddPage()
    {
        $data = Request::instance()->only('id');
        return view('add_page',compact("data"));
    }
    //培训机构添加评论
    public function trainCommentAdd(Request $request)
    {
        if($request->isAjax()){
            $data = TrainComment::trainCommentAdd();
            if(!$data){
                throw new TrainException([
                    'msg'=>'内容信息添加失败',
                    'errorCode'=>400]);
            }
            return json(['msg'=>'添加成功','code'=>200]);
        }
    }
    //删除评论
    public function trainCommentDel(Request $request)
    {
        if($request->isAjax()){
            $id = Request::instance()->only("id");
            $data = TrainComment::where('id',$id['id'])->delete();
            if(!$data){
                throw new TrainException([
                    'msg'=>'删除失败',
                    'errorCode'=>400]);}
            return json(['msg'=>'删除成功','code'=>200]);
        }
    }
    //更新培训机构课程信息的页面
    public function trainLessonUpdatePage()
    {
        $data = Request::instance()->only('id');
        return view('update',compact('data'));
    }
    //培训机构添加课程页面
    public function trainLessonAddPage()
    {
        $data  = Request::instance()->only('id');
        return view('lesson_add',compact('data'));
    }
    //培训机构添加课程
    public function trainLessonAdd(Request $request)
    {
        if($request->isAjax()){
            $data = TrainLesson::trainLessonAdd();
            if(!$data){
                throw new TrainException([
                    'msg'=>'添加失败',
                    'errorCode'=>400]);}
            return json(['msg'=>'添加成功','code'=>200]);
        }
    }
    //培训机构详细信息页面
    public function trainMessagePage()
    {
        $data =  TrainModel::trainMessagePage();
        return view("train_message",compact("data"));
    }
    //更新培训机构信息
    public function trainMessageUpdate(Request $request)
    {
        if($request->isAjax()){
            $data = TrainModel::trainMessageUpdate();
            if(!$data){
                throw new TrainException([
                    'msg'=>'更新信息失败',
                    'errorCode'=>400
                ]);
            }
            return json(['msg'=>'更新信息成功','code'=>200]);
        }

    }
    //培训机构相关评论的详细信息
    public function trainCommentDetail()
    {
        $id = Request::instance()->only('id');
        $data = TrainComment::where('id',$id['id'])->find();
        return view('detail',compact("data"));
    }
    //添加培训页面
    public function add()
    {
        $info = CustomerModel::field('id,name')->select();
        return view('add',compact('info'));
    }
    //添加培训机构
    public function trainInsert()
    {
        (new TrainVaildate())->insert();
        return TrainService::trainInsert();
    }
}

